var successDialog;
var failDialog;

$(document).ready(function(){

	$(document).keydown(function(event){ 
		if(event.keyCode == 13){ //绑定回车 
			$('#search').click(); 
		} 
	}); 

	successDialog = dialog({
		content: '操作成功！',
	});

	failDialog = dialog({
		content: '操作失败！',
		okValue: '确定',
		ok: function () {
			this.close();
		}
	});
	
	$('#add_good').click(function(){
		var html = `
			<form role="form">
			<div class="form-group">
			<label for="good_name">商品名称</label>
			<input type="text" class="form-control" id="good_name" placeholder="商品名称...">
			</div>
			<div class="form-group">
			<label for="good_description">商品描述</label>
			<input type="text" class="form-control" id="good_description" placeholder="商品描述...">
			</div>
			<div class="form-inline">
				<label for="good_img">上传商品图片</label>
				<div class="form-group">
                    <input id="good_img" type="file" name="file" multiple class="file" data-overwrite-initial="false" data-min-file-count="2">
               	</div>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" value="电子产品"> 电子产品
				</label>
				<label>
					<input type="checkbox" value="书籍"> 书籍
				</label>
				<label>
					<input type="checkbox" value="iphoneX"> iphoneX
				</label>
			</div>
			</form>
			`;
		dialog({
			title:'添加商品',
			content: html,
			okValue: '确定',
			
			ok: function () {
				this.close();
			}
		}).show();;
		$("#good_img").fileinput({
	        uploadUrl: 'good/file', // you must set a valid URL here else you will get an error
	        allowedFileExtensions : ['jpg', 'png','gif'],
	        overwriteInitial: false,
	        maxFileSize: 30000,
	        maxFilesNum: 10,
	        uploadAsync: true, //默认异步上传
            showUpload:true, //是否显示上传按钮
            showRemove :true, //显示移除按钮
            showPreview :true, //是否显示预览
            showCaption:false,//是否显示标题
	        //allowedFileTypes: ['image', 'video', 'flash'],
	        slugCallback: function(filename) {
	            return filename.replace('(', '_').replace(')', '_');
	        }
		});
		$("#good_img").on("fileuploaded", function (event, data, previewId, index) {
			console.info(data);
		})
	});
	
	

	var jsonMsg = {
			"name":$("#good_name").val(),
	}
	$.ajax({
		url:"good/list",
		data:JSON.stringify(jsonMsg),
		contentType:"application/json;charset=utf-8",
		type:"post",
		success:function(data,status){
			if(status != 'success'){
				errorDialog.show();
			}else if(data.result){
				var row = Math.ceil(data.rows.length/4);
				var html = "";
				for(var currentRow = 0 ; currentRow < row ; currentRow++){
					html += `<div class="row" style="margin-bottom: 5px;">`;
					for(var i = currentRow*4 ; i< (currentRow+1)*4 && i < data.rows.length ; i++){
						html += `<div class="col-md-3">
							<div class="sm-st">
							<img class="img-thumbnail" style="height: 180px"
							src="img/test.jpg">
							<div style="float: right">
							<a href="javascript:void(0);" class="tooltip-test" data-toggle="tooltip"
							title="加入购物车"><i class="fa fa-shopping-cart fa-2x"></i></a><br>
							<a href="javascript:void(0);" class="tooltip-test" data-toggle="tooltip"
							title="查看详情"><i class="fa fa-eye fa-2x"></i></a><br>
							</div>
							<div>
							<span class="good-left">已售:` + data.rows[i].sell + `</span> &nbsp;&nbsp;&nbsp;&nbsp; <span
							class="good-right">库存:` + data.rows[i].stock + `</span>
							</div>
							<div class="sm-st-info">
							<span>` + data.rows[i].name + `</span> 
							商品描述：` + data.rows[i].description + `
							</div>
							</div>
							</div>`;
					}
					html += `</div>`;
				}
				$('.right-side').append(html);
				$("[data-toggle='tooltip']").tooltip();
			}else{
				failDialog.show();
			}
		}
	});

	$('#search').click(function(){
		$(".table_list").bootstrapTable('refresh');
	}); 

	$('#reset').click(function(){
		$(".table_list").bootstrapTable('refresh');
	});
});

function previewFile() {
	 var preview = document.querySelector('img');
	 var file  = document.querySelector('input[type=file]').files[0];
	 var reader = new FileReader();
	 reader.onloadend = function () {
		  preview.src = reader.result;
		 }
	 if (file) {
		  reader.readAsDataURL(file);
		 } else {
			  preview.src = "";
			 }
}